package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    //根据name分页查询
    @Override
    public Page findByPage(Integer pageNum, Integer pageSize, String name) {
        //分页查询菜品信息
        //分页条件
        Page<Dish> page = new Page<>(pageNum, pageSize);
        //业务条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNoneEmpty(name),Dish::getName,name);
        //执行查询
        page = dishMapper.selectPage(page,wrapper);

        //遍历获取每个菜品
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)){
            for (Dish dish : dishList) {
                //根据菜品categoryId查询它的分类名称
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());


            }
        }
        System.out.println(page);
        return page;
    }

    //保存
    @Override
    public void save(Dish dish) {
        //保存菜品信息
        dishMapper.insert(dish);
        //获取口味列表
        if (CollectionUtil.isNotEmpty(dish.getFlavors())){
            for (DishFlavor flavor : dish.getFlavors()) {
                flavor.setDishId(dish.getId());
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    @Override
    public Dish findById(Long id) {
        //主键查询,获取菜品信息
        Dish dish = dishMapper.selectById(id);
        //根据菜品categoryId查询它的分类名称
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        //根据菜品id查询口味
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
        dish.setFlavors(dishFlavors);
        return dish;
    }

    @Override
    public void update(Dish dish) {
        //根据菜品id修改菜品信息
        dishMapper.updateById(dish);
        //根据菜品id删除口味信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);

        //遍历传入的口味列表,重新保存
        if (CollectionUtil.isNotEmpty(dish.getFlavors())){
            for (DishFlavor flavor : dish.getFlavors()) {
                //为口味设置菜品id
                flavor.setDishId(dish.getId());
                //保存口味
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        //如果有一个在售则不能删除
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getStatus,1);
        wrapper.in(Dish::getId,ids);
        Integer count = dishMapper.selectCount(wrapper);
        if (count > 0){
            throw new CustomException("当前选择菜品中有在售状态的,不可删除");
        }
        if (CollectionUtil.isNotEmpty(ids)){
            for (Long id : ids) {
                //删除当前菜品的口味列表
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,id);
                dishFlavorMapper.delete(wrapper1);
                //根据菜品id删除当前菜品信息
                dishMapper.deleteById(id);
            }
        }
    }



    //查询指定分类下菜品
    @Override
    public List<Dish> findList(Long categoryId, String name) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getStatus,1)
                .eq(categoryId != null,Dish::getCategoryId,categoryId)
                .like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //执行查询
        List<Dish> dishList = dishMapper.selectList(wrapper);

        //查询每个菜品的口味信息
        if (CollectionUtil.isNotEmpty(dishList)){
            for (Dish dish : dishList) {
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId,dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);
            }
        }
        return dishList;
    }

    //批量起售/停售
    @Override
    public void updateStatus(List<Long> ids, Integer status) {
        //设置where部分
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId, ids);
        //设置set部分
        Dish dish = new Dish();
        dish.setStatus(status);
        //更新
        dishMapper.update(dish, wrapper);
    }
}
